package org.jeecg.modules.xindian.reconciliation.entity;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysCategoryModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.xindian.reconciliation.enums.ChannelSettlementConfigType;
import org.jeecg.modules.xindian.reconciliation.enums.DeleteFlagEnum;
import org.springframework.format.annotation.DateTimeFormat;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecg.common.aspect.annotation.Dict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * @Description: 渠道结算配置
 * @Author: jeecg-boot
 * @Date:   2024-04-11
 * @Version: V1.0
 */
@Data
@TableName("channel_settlement_config")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="channel_settlement_config对象", description="渠道结算配置")
public class ChannelSettlementConfig implements Serializable {
    private static final long serialVersionUID = 1L;

	/**id*/
	@TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "id")
    private java.lang.String id;
	/**创建日期*/
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "创建日期")
    private java.util.Date createTime;
	/**更新日期*/
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "更新日期")
    private java.util.Date updateTime;
	/**渠道*/
	@Excel(name = "渠道", width = 15)
    @ApiModelProperty(value = "渠道")
    private java.lang.String channel;

    /**渠道*/
    @Excel(name = "配置信息(json非前端)", width = 15)
    @ApiModelProperty(value = "配置信息(json非前端)")
    private java.lang.String config;

    @TableField(exist = false)
    @ApiModelProperty(value = "配置信息")
    private List<Config> configs;
	/**是否删除*/
	@Excel(name = "是否删除", width = 15)
    @ApiModelProperty(value = "是否删除")
    private DeleteFlagEnum deleteFlag;

    /**结算比例*/
    @Excel(name = "发票点数", width = 15)
    @ApiModelProperty(value = "发票点数")
    private java.math.BigDecimal invoiceRate;
    @ApiModelProperty(value = "结算区域列表")
    @TableField(exist = false)
    private List<String> areas;

    public void buildConfig(){
        if(CollectionUtils.isNotEmpty(configs)){
            config = JSON.toJSONString(configs);
        }
    }

    public void buildConfigBean(){
        if(StringUtils.isNotBlank(config)){
            configs = JSON.parseArray(config,Config.class);
        }
    }

    public void buildAreas() {
        ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
        List<SysCategoryModel> sysCategoryModels = sysBaseAPI.queryAllSysCategory();
        Map<String, SysCategoryModel> sysCategoryModelMap =
                sysCategoryModels.stream().filter(sysCategoryModel -> sysCategoryModel.getCode().startsWith("B04")).collect(Collectors.toMap(SysCategoryModel::getUserCode, Function.identity()));
//        if (CollectionUtils.isNotEmpty(configs)) {
//            areas = configs.stream().map(config -> sysCategoryModelMap.get(config.area).getName()).collect(Collectors.toList());
//        }
    }

    @Data
    public static class Config{
        /**结算平台*/
        @Excel(name = "平台", width = 15)
        @ApiModelProperty(value = "结算平台")
        private List<String> platform;
        @ApiModelProperty("保险公司")
        private String insuranceCompany;
        /**结算类型*/
        @Excel(name = "结算类型", width = 15)
        @ApiModelProperty(value = "结算类型")
        private ChannelSettlementConfigType type;
        /**结算比例*/
        @Excel(name = "结算比例", width = 15)
        @ApiModelProperty(value = "结算比例")
        private java.math.BigDecimal fixRate;
        /**结算其他保单比例*/
        @Excel(name = "结算其他保单比例", width = 15)
        @ApiModelProperty(value = "结算其他保单比例")
        private java.math.BigDecimal otherPolicyRate;
    }


}
